package com.sctvcloud.dongqu.ui.adapter.holder;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.ruihang.generalibrary.ui.adapter.holder.BaseRecylerHolder;
import com.ruihang.generalibrary.ui.widget.CustomCheckBox;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.sctvcloud.dongqu.R;
import com.sctvcloud.dongqu.beans.AFileItem;
import com.sctvcloud.dongqu.beans.AuthorFileItem;
import com.sctvcloud.dongqu.beans.FProgramme;
import com.sctvcloud.dongqu.ui.adapter.AnchorHomePageAdapter;
import com.sctvcloud.dongqu.ui.adapter.AuthorAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuxipan on 2018-01-15.
 */

public class AuthorFileHolder extends BaseAnchorHolder<AuthorFileItem> implements CompoundButton.OnCheckedChangeListener {

    @BindView(R.id.layout_title)
    protected View headView;
    @BindView(R.id.iv_item_anchor_title)
    protected ImageView ivHeadView;
    @BindView(R.id.tv_item_anchor_title_name)
    protected CustomFontTextView tvHeadText;
    @BindView(R.id.rb_auchor_file_sctv)
    protected RadioButton cbSctvTab;
    @BindView(R.id.rb_auchor_file_other)
    protected RadioButton cbOtherTab;


    @BindView(R.id.rcv_author_afile_list)
    protected RecyclerView ryList;
    @BindView(R.id.tv_empty)
    protected CustomFontTextView tvEmpty;

    @BindView(R.id.tv_anchor_file_more)
    protected CustomFontTextView tvMore;

    private AuthorAdapter authorAdapter;
    private AuthorFileItem item;
    private boolean isSctv = true;


    public AuthorFileHolder(Context context, View view) {
        super(context, view);
        cbSctvTab.setChecked(true);
        tvMore.setTag(isSctv);
        cbOtherTab.setOnCheckedChangeListener(this);
        cbSctvTab.setOnCheckedChangeListener(this);
        GridLayoutManager layoutManager = new GridLayoutManager(context, 5);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        ryList.setLayoutManager(layoutManager);
        authorAdapter = new AuthorAdapter(context, new ArrayList<AFileItem>());
        authorAdapter.setActivity(activity);
        authorAdapter.setFragment(fragment);
        authorAdapter.setPairs(pairs);
        ryList.setAdapter(authorAdapter);
        tvMore.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (internalClick != null) {
                    internalClick.onItemInternalClick(itemView, v, getAdapterPosition());
                }
            }
        });
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(AuthorFileItem item) {
        this.item = item;
        itemView.setTag(AnchorHomePageAdapter.HAS_STICKY_VIEW);
        ivHeadView.setVisibility(View.GONE);
        tvHeadText.setText("主播档案");
        itemView.setContentDescription("主播档案");
        if (isSctv) {
            authorAdapter.setData(item.getSctvFileItem());
            if (item.isSctvMore()) {
                tvMore.setVisibility(View.VISIBLE);
            } else {
                tvMore.setVisibility(View.GONE);
            }
        } else {
            authorAdapter.setData(item.getOtherFileItem());
            if (item.isOtherMore()) {
                tvMore.setVisibility(View.VISIBLE);
            } else {
                tvMore.setVisibility(View.GONE);
            }
        }


    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if (isChecked) {
            switch (buttonView.getId()) {
                case R.id.rb_auchor_file_sctv:
                    if (item.getSctvFileItem() == null) {
                        if (internalClick != null) {
                            internalClick.onItemInternalClick(itemView, buttonView, getAdapterPosition());
                        }
                    } else {
                        if (item.getSctvFileItem().size() > 0) {
                            ryList.setVisibility(View.VISIBLE);
                            tvEmpty.setVisibility(View.GONE);
                            authorAdapter.setData(item.getSctvFileItem());
                            if (item.isSctvMore()) {
                                tvMore.setVisibility(View.VISIBLE);
                            } else {
                                tvMore.setVisibility(View.GONE);
                            }
                            isSctv = true;
                            tvMore.setTag(isSctv);
                        } else {
                            ryList.setVisibility(View.GONE);
                            tvEmpty.setVisibility(View.VISIBLE);
                            tvMore.setVisibility(View.GONE);
                        }

                    }

                    break;
                case R.id.rb_auchor_file_other:
                    if (item.getOtherFileItem() == null) {
                        if (internalClick != null) {
                            internalClick.onItemInternalClick(itemView, buttonView, getAdapterPosition());
                        }
                    } else {
                        if (item.getOtherFileItem().size() > 0) {
                            ryList.setVisibility(View.VISIBLE);
                            tvEmpty.setVisibility(View.GONE);

                            authorAdapter.setData(item.getOtherFileItem());
                            if (item.isOtherMore()) {
                                tvMore.setVisibility(View.VISIBLE);
                            } else {
                                tvMore.setVisibility(View.GONE);
                            }
                            isSctv = false;
                            tvMore.setTag(isSctv);
                        } else {
                            ryList.setVisibility(View.GONE);
                            tvEmpty.setVisibility(View.VISIBLE);
                            tvMore.setVisibility(View.GONE);
                        }
                    }

                    break;

            }
        }

    }
}
